import {toJS} from 'mobx';
import {inject, observer} from 'mobx-react'
import {Row, Col, Switch, Tag, DatePicker, Tabs, Icon, Form, Input, TimePicker, Button} from 'antd';
import {
  WeaQrcode,
  WeaDialog,
  WeaBrowser,
  WeaNewScroll,
  WeaTextarea,
  WeaTableEdit,
  WeaInput,
  WeaUpload,
  WeaFormItem,
  WeaLocaleProvider
} from 'ecCom';
import objectAssign from 'object-assign'
import Collapse from "../../collapse.js"
import mapKeys from 'lodash/mapKeys';
import mapValues from 'lodash/mapValues';

const getLabel = WeaLocaleProvider.getLabel;
let _this = null;

@inject('meetingDialog')
@observer
class DecisionDialog extends React.Component {
  constructor(props) {
    super(props);
    _this = this;
    this.state={
      initCodeNum : 0,
    }
    // this.state = {
    // 	decision: '',
    // 	decisiondoc: [],//文档
    // 	decisioncrmids: [],//客户
    // 	decisionprjids: [],//项目
    // 	decisiontskids: [],//任务
    // 	decisionwfids: [],//流程
    // 	decisionatchids: [],//附件
    // 	// datas: [],//下面的table
    //
    // 	decisionStatus: ''
    // }
  }

  shouldComponentUpdate(nextProps, nextState) {
    //组件渲染控制
    //return this.props.title !== nextProps.title
    return true;
  }

  componentDidMount() {
    const {meetingDialog} = this.props;
    const decisionStatus = toJS(meetingDialog.decisionStatus);
    const initlen = decisionStatus.datas.length
    this.setState({initCodeNum:initlen})
    // let { decisionStatus } = this.props;
    // this.setState({
    // 	decision: decisionStatus.decision,
    // 	decisiondoc: decisionStatus.decisiondoc,
    // 	decisioncrmids: decisionStatus.decisioncrmids,
    // 	decisionprjids: decisionStatus.decisionprjids,
    // 	decisiontskids: decisionStatus.decisiontskids,
    // 	decisionwfids: decisionStatus.decisionwfids,
    // 	decisionatchids: decisionStatus.decisionatchids,
    // 	datas: decisionStatus.datas
    // })
  }

  componentWillReceiveProps(nextProps) {
    const {meetingDialog} = this.props;
    const {setState} = meetingDialog;
    let dialogTypeTemp = "decision";
    setState({dialogType:dialogTypeTemp});
    // let { decisionStatus } = nextProps
    // if (this.state.decisionStatus != nextProps.decisionStatus) {
    // 	this.setState({
    // 		decision: decisionStatus.decision,
    // 		decisiondoc: decisionStatus.decisiondoc,
    // 		decisioncrmids: decisionStatus.decisioncrmids,
    // 		decisionprjids: decisionStatus.decisionprjids,
    // 		decisiontskids: decisionStatus.decisiontskids,
    // 		decisionwfids: decisionStatus.decisionwfids,
    // 		decisionatchids: decisionStatus.decisionatchids,
    // 		datas: decisionStatus.datas,
    // 		decisionStatus: nextProps.decisionStatus
    //
    // 	})
    // }
  }

  render() {
    const {meetingDialog, isSingle} = this.props;
    const decisionStatus = toJS(meetingDialog.decisionStatus);
    const {
      decision = '',
      decisiondoc = [],//文档
      decisioncrmids = [],//客户
      decisionprjids = [],//项目
      decisiontskids = [],//任务
      decisionwfids = [],//流程
      decisionatchids = [],//附件
      datas = [],//下面的table
      isPro,
    } = toJS(meetingDialog.decisionStatus)

    let browserParam = {
      hasAddBtn: false,
      hasAdvanceSerach: true,
      isAutoComplete: 1,
      isDetail: 0,
      isMultCheckbox: false,
      isSingle: false,
      linkUrl: "/hrm/resource/HrmResource.jsp?id=",
      quickSearchName: "",
      title: getLabel(839, "多人力资源"),
      type: "17",
      viewAttr: 2,
      layout: document.body
    }
    let browserParam2 = {
      hasAddBtn: false,
      hasAdvanceSerach: true,
      idSeparator: ",",
      isAutoComplete: 1,
      isDetail: 0,
      isMultCheckbox: false,
      isSingle: true,
      linkUrl: "/hrm/resource/HrmResource.jsp?id=",
      pageSize: 10,
      quickSearchName: "",
      title: getLabel(383424, "人力资源"),
      type: "1",
      viewAttr: 2,
      layout: document.body
    }

    let tableColumns = [
      {
        title: getLabel(383346, "编号"),
        dataIndex: 'coding',
        key: 'coding',
        colSpan: 1,
        className: 'coding',
        com: [
          {label: '', type: 'INPUT', key: 'coding', viewAttr: 3},
        ],
        width: '16.6%',

      },
      {
        title: getLabel(229, "标题"),
        dataIndex: 'subject',
        key: 'subject',
        colSpan: 1,
        className: 'subject',
        com: [
          {label: '', type: 'INPUT', key: 'subject', viewAttr: 3},
        ],
        width: '16.6%',

      },
      {
        title: getLabel(383489, "执行人"),
        dataIndex: 'hrmid01',
        key: 'hrmid01',
        colSpan: 1,
        className: 'hrmid01',
        com: [
          {label: '', type: 'BROWSER', key: 'hrmid01', browserConditionParam: browserParam, viewAttr: 3},
        ],
        width: '16.6%',

      },
      {
        title: getLabel(125149, "检查人"),
        dataIndex: 'hrmid02',
        key: 'hrmid02',
        colSpan: 1,
        className: 'hrmid02',
        com: [
          {label: '', type: 'BROWSER', key: 'hrmid02', browserConditionParam: browserParam2},
        ],
        width: '16.6%',

      },
      {
        title: getLabel(83825, "开始时间"),
        dataIndex: 'begindate',
        key: 'begindate',
        colSpan: 1,
        className: 'begindate',
        com: [
          {label: '', type: 'DATEPICKER', key: 'begindate', showTime: true, viewAttr: 3, format: "yyyy-MM-dd HH:mm"},
        ],
        width: '16.6%',

      },
      {
        title: getLabel(83826, "结束时间"),
        dataIndex: 'enddate',
        key: 'enddate',
        colSpan: 1,
        className: 'enddate',
        com: [
          {label: '', type: 'DATEPICKER', key: 'enddate', showTime: true, viewAttr: 3, format: "yyyy-MM-dd HH:mm"},
        ],
        width: '16.6%',
      },
    ];
    return (
      <div className="preview-decision-dialog" style={{height: isSingle ? "100%": this.props.contentHeight, overflow: 'auto'}}>
        <Collapse ecId={`${this && this.props && this.props.ecId || ''}_Collapse@lfc0oj`}
          title={getLabel(383490, "一般")}
          showGroup={true}
        >
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@mhff4m`}
            label={getLabel(2170, "决议概述")}
            labelCol={{span: '6', offset: '2'}}
            wrapperCol={{span: '11'}}>
            <Input ecId={`${this && this.props && this.props.ecId || ''}_Input@36thnb`}
              type="textarea"
              value={decision}
              onChange={(e) => {
                // this.setState({decision: e.target.value})
                meetingDialog.setDecisionStatus({decision: e.target.value})
              }}/>
          </WeaFormItem>

        </Collapse>
        {
          decisionStatus && decisionStatus.showset ?
            <Collapse ecId={`${this && this.props && this.props.ecId || ''}_Collapse@qf8hor`}
              title={getLabel(383491, "相关信息")}
              showGroup={true}
            >
              {//文档
                decisionStatus && decisionStatus.showset.doc ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@wc8nuz`}
                  label={getLabel(857, "相关文档")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@4zn737`}
                    replaceDatas={decisiondoc}
                    onChange={(ids, names, datas) => {
                      // this.setState({ decisiondoc: datas })
                      meetingDialog.setDecisionStatus({decisiondoc: datas})
                    }}
                    hasAddBtn={false}
                    hasAdvanceSerach={true}
                    idSeparator=","
                    isAutoComplete={1}
                    isDetail={0}
                    isMultCheckbox={false}
                    isSingle={false}
                    linkUrl="/spa/document/index.jsp?id="
                    pageSize={10}
                    quickSearchName=""
                    title={getLabel(126529, "文档")}
                    type="37"
                    viewAttr={2}
                    completeParams={{secretLevel:meetingDialog.oldSecretLevel}}
                    dataParams={{secretLevel:meetingDialog.oldSecretLevel}}
                  />
                </WeaFormItem> : ''
              }
              {//流程
                decisionStatus && decisionStatus.showset.wf ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@2lb75t`}
                  label={getLabel(1044, "相关流程")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@ej4hbz`}
                    replaceDatas={decisionwfids}
                    onChange={(ids, names, datas) => {
                      // this.setState({decisionwfids: datas})
                      meetingDialog.setDecisionStatus({decisionwfids: datas})
                    }}
                    hasAddBtn={false}
                    hasAdvanceSerach={true}
                    idSeparator=","
                    isAutoComplete={1}
                    isDetail={0}
                    isMultCheckbox={false}
                    isSingle={false}
                    linkUrl="/spa/workflow/index_form.jsp#/main/workflow/req?requestid="
                    pageSize={10}
                    quickSearchName=""
                    tabs={[{
                      isSearch: 'false',
                      key: "1",
                      name: getLabel(82857, "全部"),
                      selected: 'false'
                    }, {isSearch: 'false', key: "2", name: getLabel(383492, "我的收藏"), selected: 'false'}]}
                    title={getLabel(131412, "流程")}
                    type="152"
                    viewAttr={2}
                    completeParams={{secretLevel:meetingDialog.oldSecretLevel}}
                    dataParams={{secretLevel:meetingDialog.oldSecretLevel}}
                  />
                </WeaFormItem> : ''
              }
              {//客户
                decisionStatus && decisionStatus.showset.crm ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@jpyxs1`}
                  label={getLabel(783, "相关客户")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@abs2rn`}
                    replaceDatas={decisioncrmids}
                    onChange={(ids, names, datas) => {
                      // this.setState({decisioncrmids: datas})
                      meetingDialog.setDecisionStatus({decisioncrmids: datas})
                    }}
                    hasAddBtn={false}
                    hasAdvanceSerach={true}
                    idSeparator=","
                    isAutoComplete={1}
                    isDetail={0}
                    isMultCheckbox={false}
                    isSingle={false}
                    linkUrl={isPro?'/CRM/manage/customer/CustomerBaseView.jsp?CustomerID=':"/spa/crm/static/index.html#/main/crm/customerView?customerId="}
                    pageSize={10}
                    quickSearchName=""
                    title={getLabel(30043, "客户")}
                    type="18"
                    viewAttr={2}
                  />
                </WeaFormItem> : ''
              }
              {//项目
                decisionStatus && decisionStatus.showset.prj ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@hi5qgs`}
                  label={getLabel(782, "相关项目")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@zi1wiu`}
                    replaceDatas={decisionprjids}
                    onChange={(ids, names, datas) => {
                      // this.setState({decisionprjids: datas})
                      meetingDialog.setDecisionStatus({decisionprjids: datas})
                    }}
                    hasAddBtn={false}
                    hasAdvanceSerach={true}
                    idSeparator=","
                    isAutoComplete={1}
                    isDetail={0}
                    isMultCheckbox={false}
                    isSingle={false}
                    linkUrl="/spa/prj/index.html#/main/prj/projectCard?prjid="
                    pageSize={10}
                    quickSearchName=""
                    title={getLabel(30046, "项目")}
                    type="135"
                    viewAttr={2}
                  />
                </WeaFormItem> : ''
              }
              {//任务
                decisionStatus && decisionStatus.showset.tsk ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@v79vsn`}
                  label={getLabel(33414, "相关任务")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@un2zsv`}
                    replaceDatas={decisiontskids}
                    onChange={(ids, names, datas) => {
                      // this.setState({decisiontskids: datas})
                      meetingDialog.setDecisionStatus({decisiontskids: datas})
                    }}
                    isSingle={false}
                    title={getLabel(383349, "任务")}
                    linkUrl="/spa/prj/index.html#/main/prj/taskCard?taskid="
                    type="prjtsk"
                    viewAttr={2}
                  />
                </WeaFormItem> : ''
              }
              {//附件
                decisionStatus && decisionStatus.showset.atch ? <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@zhxyll`}
                  label={getLabel(22194, "相关附件")}
                  labelCol={{span: '6', offset: '2'}}
                  wrapperCol={{span: '11'}}>
                  <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@tie0bc`}
                    maxUploadSize={decisionStatus.showset.atch && decisionStatus.showset.atch.maxUploadSize}//最大上传大小限制，单位为MB
                    mixUploadSize={decisionStatus.showset.atch && decisionStatus.showset.atch.mixUploadSize}//最小上传大小限制，单位为MB
                    btnSize="default"//按钮大小
                    uploadUrl={`${decisionStatus.showset.atch && decisionStatus.showset.atch.uploadUrl}${decisionStatus.showset.atch.uploadUrl.indexOf("category")<0?("?category="+decisionStatus.showset.atch.category):""}`}//文件上传服务器接口地址
                    category={`${decisionStatus.showset.atch && decisionStatus.showset.atch.category}`}//文件上传目录
                    onUploading={() => {
                    }}//文件上传状态接收函数
                    datas={decisionatchids}
                    onChange={(ids, list) => {
                      // this.setState({decisionatchids: list})
                      meetingDialog.setDecisionStatus({decisionatchids: list})
                    }}
                    ref={'decisionupload'}
                    uploadId={"_decisionupload"}//上传组件标识，同时生成隐藏域 id
                    style={{display: "inline-block", padding: "0"}}
                    autoUpload={true}//自动上传
                    errorMsg={decisionStatus.showset.atch && decisionStatus.showset.atch.errorMsg}
                  />
                </WeaFormItem> : ''
              }
            </Collapse> : ""
        }
        <Collapse ecId={`${this && this.props && this.props.ecId || ''}_Collapse@rs564b`}
          title={getLabel(2171, "决议")}
          showGroup={true}
        >
          <WeaTableEdit ecId={`${this && this.props && this.props.ecId || ''}_WeaTableEdit@x02c86`}
            ref={'decisionTableEdit'}
            columns={tableColumns}
            datas={datas}
            onChange={this.onChange2}
            viewAttr={2}
            onAdd={(a,b)=>{this.onAddChange(a,b)}}
          />

        </Collapse>
      </div>
    )
  }

  returnState = () => {
    return {...this.state}
  }

  onAddChange = (index,data) => {
    const {meetingDialog} = this.props;
    if(typeof index =='object' && typeof data=='object' && index.length>0 && data.length>0){
      data[0].coding=index[0]
      const {
        datas = [],//下面的table
      } = toJS(meetingDialog.decisionStatus)
      datas[index-1]=data[0]
      this.props.meetingDialog.setDecisionStatus({datas: datas});
    }
    
  }

  onChange2 = (datas) => {
    // this.setState({ datas: datas })
    this.props.meetingDialog.setDecisionStatus({datas: datas});
  }

  onChange = (datas, column) => {
    let _datas = [];
    let _each = {};
    datas.forEach((element, index) => {
      _each = mapKeys(element, (value, key) => {
        let _key = key.split('_')[0];
        return _key;
      });
      _datas.push(_each);
    })
    const {previewFields, actions} = this.props;
    let oldpreviewFields = {...previewFields}
    let decisionrows = '';
    let each = {};
    let eachvalue = {};
    let alldatas = {};
    _datas.forEach((element, index) => {
      each = mapKeys(element, (value, key) => {
        return key + '_' + (index + 1);
      });
      eachvalue = mapValues(each, (e, i) => {
        return {
          value: e,
          name: i,
        };
      });
      alldatas = objectAssign(alldatas, eachvalue);
    });
    decisionrows = datas.length;
    alldatas = objectAssign(alldatas, oldpreviewFields, {
      decisionrows: {
        value: decisionrows || 0,
        name: 'decisionrows'
      }
    });
    actions.savePreviewFields(alldatas);
  }

}

export default DecisionDialog




